﻿using MuddyBoots.GreenlightQC.DataService.Enumeration;
using MuddyBoots.GreenlightQC.DataService.Model;
using MuddyBoots.GreenlightQC.Mobile.Persistence;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MuddyBoots.GreenlightQC.DataService
{
    public class GreenlightQCMobileMock : IGreenlightQCMobile
    {
        public Menu GetMenu(IDatabaseEntities database, int applicationIdentifier)
        {
            return new Menu
            {
                Assessments = new[]
                {
                    new MenuItem { Index = 1, TargetIdentifier = 2, Text="Arrival Check" },
                    new MenuItem { Index = 2, TargetIdentifier = 7, Text="PQI Benchmark Check" },
                    new MenuItem { Index = 3, TargetIdentifier = 14, Text="PQI Store Visit" },
                    new MenuItem { Index = 4, TargetIdentifier = 17, Text="SSC Customer Check" },
                    new MenuItem { Index = 5, TargetIdentifier = 18, Text="Visual Check" },
                    new MenuItem { Index = 6, TargetIdentifier = 19, Text="Store Customer Check" },
                    new MenuItem { Index = 7, TargetIdentifier = 20, Text="Factory Dispatch Check" },
                    new MenuItem { Index = 8, TargetIdentifier = 21, Text="Factory Customer Check" },
                    new MenuItem { Index = 9, TargetIdentifier = 22, Text="Positive Release" },
                    new MenuItem { Index = 10, TargetIdentifier = 23, Text="Factory Positive Release" },
                    new MenuItem { Index = 11, TargetIdentifier = 1, Text="PQI Facility Check" }
                },
                CorrectiveActions = new[]
                {
                    new MenuItem { Index = 1, TargetIdentifier = 2, Text="Log Corrective Action" },
                    new MenuItem { Index = 2, TargetIdentifier = 7, Text="Corrective Action Manager" }
                },
                Tasks = new[] 
                { 
                    new MenuItem { Index = 1, TargetIdentifier = 2, Text="Log Task" },
                    new MenuItem { Index = 2, TargetIdentifier = 7, Text="View Tasks" },
                    new MenuItem { Index = 3, TargetIdentifier = 14, Text="QC Actions" },
                    new MenuItem { Index = 4, TargetIdentifier = 17, Text="Specifications To Sync" }
                },
                Reports = new[]
                {
                    new MenuItem { Index = 1, TargetIdentifier = 1, Text="Stop Fretting" },
                    new MenuItem { Index = 2, TargetIdentifier = 2, Text="It's all fake" }
                },
                Synchronise = new[] { new MenuItem { Index = 1, TargetIdentifier = 1, Text="Synchronise" } }
            };
        }

        public AssessmentType GetAssessmentType(IDatabaseEntities database, int assessmentType)
        {
            if (assessmentType < 0)
            {
                return null;
            }

            var questions = new List<Question>();
            questions.Add(new Question { Type = typeof(string), IsRequired=true, QuestionText = "Supplier Name" });
            questions.Add(new Question { Type = typeof(string), IsRequired = true, QuestionText = "Distribution Centre" });
            questions.Add(new Question { Type = typeof(string), QuestionText = "Purchase Order" });
            questions.Add(new Question { Type = typeof(string), IsRequired = true, QuestionText = "Order Item Code" });
            questions.Add(new Question { Type = typeof(string), QuestionText = "Grower" });
            questions.Add(new Question { Type = typeof(DateTime), QuestionText = "Harvest Date" });
            questions.Add(new Question { Type = typeof(int), QuestionText = "Quantity Ordered" });
            questions.Add(new Question { Type = typeof(DateTime), QuestionText = "Packed On Date" });
            questions.Add(new Question { Type = typeof(int), QuestionText = "Quantity Received" });
            questions.Add(new Question { Type = typeof(DateTime), QuestionText = "Arrival Date" });
            questions.Add(new Question { Type = typeof(int), IsRequired = true, QuestionText = "Quantity Checked" });
            questions.Add(new Question { Type = typeof(int), QuestionText = "Watchlist Status" });

            var previousChecks = new List<PreviousCheck>();
            previousChecks.Add(new PreviousCheck { CheckId = 1, AssessmentName = "Green Apples", Supplier = "Will Hunting", StockCode = "123456", Location = "USA", Grower = "Will Hunting", FullName = "Good Will hunting", HarvestDate = new DateTime(2015, 5, 1), ActionReason = "How'd ya like them apples?", Flag = Flag.Green });
            previousChecks.Add(new PreviousCheck { CheckId = 1, AssessmentName = "Green Apples", Supplier = "Will Hunting", StockCode = "123456", Location = "USA", Grower = "Will Hunting", FullName = "Good Will hunting", HarvestDate = new DateTime(2015, 5, 1), ActionReason = "How'd ya like them apples?", Flag = Flag.Amber });
            previousChecks.Add(new PreviousCheck { CheckId = 1, AssessmentName = "Green Apples", Supplier = "Will Hunting", StockCode = "123456", Location = "USA", Grower = "Will Hunting", FullName = "Good Will hunting", HarvestDate = new DateTime(2015, 5, 1), ActionReason = "How'd ya like them apples?", Flag = Flag.Red });

            var assessmentHeader = new List<Question>();
            assessmentHeader.Add(new Question { Type = typeof(string), IsRequired = true, QuestionText = "Supplier Name" });
            assessmentHeader.Add(new Question { Type = typeof(string), IsRequired = false, QuestionText = "Distribution Centre" });
            assessmentHeader.Add(new Question { Type = typeof(string), IsRequired = false, QuestionText = "Purchase Order" });
            assessmentHeader.Add(new Question { Type = typeof(string), IsRequired = true, QuestionText = "Order Item Code" });
            
            return new AssessmentType
            {
                Assessment = questions,
                HeaderQuestions = assessmentHeader,
                PreviousChecks = previousChecks,
                AssessmentName = "Green Apples"
            };
        }

        public void SaveAssessment(IDatabaseEntities database, IReadOnlyCollection<Question> assessment)
        {
            throw new NotImplementedException();
        }

        public User LogIn(IDatabaseEntities database, string username, string password)
        {
            if (username.Equals("muddy", StringComparison.InvariantCultureIgnoreCase) && password.Equals("backdoor"))
            {
                var result = new User(1, "Isaac Newton", 1, "isaac", 1, "en-GB");
                result.AccountStatus = UserAccountStatus.Valid;
                return result;
            }

            throw new UnauthorizedAccessException("The credentials do not match a valid user");
        }
    }
}
